package com.keeson.android_cli.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class SpreadView extends View {

    private int centerX ;
    private int centerY ;
    private int maxRaduis = 1500 ; //最大半径 px
    private int cycle = 2000 ;  //周期毫秒
    private int interval = 800;  //生成间隔  毫秒
    private List<SpreadCircle> circles = new ArrayList<>();
    private List<SpreadCircle> circlesPool = new ArrayList<>();
    private long startTime ;
    private long lastTime = 0l;
    private Paint paint ;

    private int alpha1 = 40;

    public SpreadView(Context context) {
        this(context,null);
    }

    public SpreadView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SpreadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(0xff81B1FF);
       // paint.setStyle(Paint.Style.STROKE);
        paint.setStyle(Paint.Style.FILL);
        //开始计时器 生成圆 和 刷新圆的状态
        startTime = System.currentTimeMillis();

    }

    private void freshCircles() {
        long currentTimeMillis = System.currentTimeMillis();
        if(currentTimeMillis - lastTime > 600){
            lastTime = currentTimeMillis;
            if(circlesPool.isEmpty()){
                circles.add(new SpreadCircle(0,currentTimeMillis,alpha1));
            }else {
                circles.add(circlesPool.get(0).resetCircle(0,currentTimeMillis,alpha1));
                circlesPool.remove(0);
            }
        }
        for (SpreadCircle circle : circles) {
            int currentRaduis = (maxRaduis*(int) (currentTimeMillis - circle.bornTime)/cycle);
            circle.raduis = currentRaduis;
            circle.alpha = alpha1 - (alpha1*(int) (currentTimeMillis - circle.bornTime)/cycle);
        }
        if(circles.get(0).raduis > 1500){
            SpreadCircle oldCircle = circles.get(0);
            circles.remove(0);
            circlesPool.add(oldCircle);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w;
        centerY = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        freshCircles();
        for (SpreadCircle circle : circles) {
            paint.setAlpha(circle.alpha);
            canvas.drawCircle(centerX,centerY,circle.raduis,paint);
        }
        invalidate();
    }

    private class SpreadCircle{
        private int raduis = 0;
        private long bornTime = 0;
        private int alpha = alpha1 ;

        public SpreadCircle(int raduis, long bornTime,int alpha ) {
            this.raduis = raduis;
            this.bornTime = bornTime;
            this.alpha = alpha;
        }

        private SpreadCircle resetCircle(int raduis, long bornTime,int alpha){
            this.raduis = raduis;
            this.bornTime = bornTime;
            this.alpha = alpha;
            return this;
        }
    }

}
